package com.example.testprog1;

import com.example.testprog1.MyTables.AccountType;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.content.*;
import android.database.sqlite.*;
import android.widget.*;

public class NewCategoryTypeActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_category_type);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.new_category_type, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(
					R.layout.fragment_new_category_type, container, false);
			return rootView;
		}
	}
	
	//khang
	public void updateCategoryType (View view){
		//Open Database
		EditText editText = (EditText) findViewById(R.id.editText1);
		//String cat_type = "" + editText.toString();

		MyDBOpenHelper dbHelper = new MyDBOpenHelper(getBaseContext());
		
		// Gets the data repository in write mode
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		
		// Create a new map of values, where column names are the keys
		ContentValues values = new ContentValues();
		
		values.put(AccountType.COLUMN_NAME_ACCOUNT_TYPE_NAME, editText.getText().toString());
		
		long newRowId;
		newRowId = db.insert(AccountType.TABLE_NAME,null, values);
		
		if (newRowId ==1){
			Context context = getApplicationContext();
			CharSequence text = String.valueOf(newRowId) + "Added";
			int duration = Toast.LENGTH_LONG;
			
			Toast toast = Toast.makeText(context, text, duration);
			toast.show();
		}
		
		editText.clearComposingText();
		
		/*
		//if (cat_type.isEmpty())		{
		if (editText.getText().toString().isEmpty())
		{
			Context context = getApplicationContext();
			CharSequence text = "Category Name mustn't empty";
			int duration = Toast.LENGTH_LONG;
			

			Toast toast = Toast.makeText(context, text, duration);
			toast.show();
		}
		else{
			Context context = getApplicationContext();
			CharSequence text = editText.getText();
			int duration = Toast.LENGTH_LONG;
			

			Toast toast = Toast.makeText(context, text, duration);
			toast.show();
				
		}
		*/
		
	}

}
